package com.siwei.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.Equipment;
import com.siwei.mes.entity.Experiment;
import com.siwei.mes.entity.ExperimentSampleSmoke;
import com.siwei.mes.entity.TemperatureAndHumidity;
import com.siwei.mes.entity.tgl.TglItemorder;
import com.siwei.mes.entity.tgl.TglTrwd;
import com.siwei.mes.enums.ExperimentStatusEnum;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.mapper.ExperimentMapper;
import com.siwei.mes.mapper.ExperimentSampleSmokeMapper;
import com.siwei.mes.service.AuthService;
import com.siwei.mes.service.EquipmentService;
import com.siwei.mes.service.ExperimentService;
import com.siwei.mes.service.TemperatureAndHumidityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账(Experiment)表服务实现类
 *
 * @author linzi
 * @since 2023-12-18 22:11:47
 */
@Service
public class ExperimentServiceImpl implements ExperimentService {
    @Resource
    private ExperimentMapper experimentMapper;
    @Resource
    private AuthService authService;
    @Resource
    private EquipmentService equipmentService;
    @Resource
    private TemperatureAndHumidityService temperatureAndHumidityService;
    @Resource
    private ExperimentSampleSmokeMapper experimentSampleSmokeMapper;

    @Override
    public Long create(Experiment entity) {
        entity.init(true);
        experimentMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(Experiment entity) {
        Experiment experiment = experimentMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experiment == null, "试验台账信息不存在");
        entity.init(false);
        experimentMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        Experiment experiment = experimentMapper.selectByPrimaryKey(id);
        assertTrue(experiment == null, "试验台账信息不存在");
        experimentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Experiment> getPage(PageQuery<Experiment, Experiment> query) {
        Experiment searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentMapper.getList(searchParam));
    }

    @Override
    public Experiment loadById(Long id) {
        return experimentMapper.loadById(id);
    }

    @Override
    public List<Experiment> getAll(Experiment entity) {
        return experimentMapper.getList(entity);
    }

    @Override
    public void accept(Long id) {
        Experiment entity = experimentMapper.selectByPrimaryKey(id);
        assertTrue(entity == null, "试验台账信息不存在");
        entity.init(false);
        entity.setAcceptPersonId(authService.getLoginUserId());
        entity.setAcceptPersonName(authService.getLoginUserName());
        entity.setAcceptTime(new Date());
        entity.setExperimentStatus(ExperimentStatusEnum.WAIT_SAMPLING.getCode());
        experimentMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void finish(Long id) {
        Experiment entity = experimentMapper.selectByPrimaryKey(id);
        assertTrue(entity == null, "试验台账信息不存在");
        entity.init(false);
        entity.setFinishPersonId(authService.getLoginUserId());
        entity.setFinishPersonName(authService.getLoginUserName());
        entity.setFinishTime(new Date());
        entity.setExperimentStatus(ExperimentStatusEnum.FINISH.getCode());

        entity.setExperimentPersonId(authService.getLoginUserId());
        entity.setExperimentPersonName(authService.getLoginUserName());
        entity.setExperimentTime(new Date());
        experimentMapper.updateByPrimaryKeySelective(entity);
        /**把抽样记录都变成已完成*/
        experimentSampleSmokeMapper.updateIsFinish(entity.getId());

        /**查询所有抽样记录 把抽样记录的处置人变成最后完成委托的人*/
        List<ExperimentSampleSmoke> sampleSmokeList = experimentSampleSmokeMapper.selectByExperimentId(id);
        for (ExperimentSampleSmoke sampleSmoke : sampleSmokeList) {
            sampleSmoke.init(false);
            sampleSmoke.setDisposerPersonId(authService.getLoginUserId());
            sampleSmoke.setDisposerPersonName(authService.getLoginUserName());
            experimentSampleSmokeMapper.updateByPrimaryKeySelective(sampleSmoke);
        }
    }

    @Override
    public void refuse(Long id) {
        Experiment entity = experimentMapper.selectByPrimaryKey(id);
        assertTrue(entity == null, "试验台账信息不存在");
        entity.init(false);
        entity.setRefusePersonId(authService.getLoginUserId());
        entity.setRefusePersonName(authService.getLoginUserName());
        entity.setRefuseTime(new Date());
        entity.setExperimentStatus(ExperimentStatusEnum.REFUSE.getCode());
        experimentMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public Experiment getExperimentInfoByItemOrderInfo(TglItemorder entity, Integer checkType, String entrustExperiment) {
        Experiment experiment = TglItemorder.payloadExperiment(entity, MaterialsEnum.CONCRETE.getCode());
        experiment.setEntrustExperiment(entrustExperiment);
        experiment.setCheckType(checkType);
        /**获取仪器设备*/
        List<Equipment> equipmentInfoList = equipmentService.getEquipmentInfo(MaterialsEnum.CONCRETE.getCode().toString());
        StringBuilder equipment = new StringBuilder();
        for (Equipment eq : equipmentInfoList) {
            equipment.append(eq.getEquipmentName()).append(",");
        }
        if (equipment.length() > 0) {
            experiment.setEquipment(equipment.deleteCharAt(equipment.length() - 1).toString());
        }
        /**获取最后一条含水率配置*/
        TemperatureAndHumidity lastInfo = temperatureAndHumidityService.getLastInfo(new TemperatureAndHumidity());
        if (lastInfo != null) {
            experiment.setTemperature(lastInfo.getTemperature());
            experiment.setHumidness(lastInfo.getHumidness());
        }
        return experiment;
    }

    @Override
    public Experiment getExperimentInfoByTglTrwdInfo(TglTrwd tglTrwd, Integer checkType) {
        Experiment experiment = new Experiment();
        experiment.tglInit(true);
        experiment.setCheckType(checkType);
        experiment.setExperimentType(MaterialsEnum.CONCRETE.getCode());
        experiment.setEntrustTime(new Date());
        experiment.setSampleLevel(tglTrwd.getFtpz());
        experiment.setExperimentGist("GB 50081-2019《混凝土物理力学性能试验方法标准》、GB/T 50082-2009《普通混凝土长期性能和耐久性能试验方法标准》");
        experiment.setBehalfNumber(0d);
        experiment.setEntrustReasonCode("GHPHB");
        experiment.setEntrustReasonName("更换配合比");
        experiment.setEntrustExperiment("更换配合比");

        /**获取仪器设备*/
        List<Equipment> equipmentInfoList = equipmentService.getEquipmentInfo(MaterialsEnum.CONCRETE.getCode().toString());
        StringBuilder equipment = new StringBuilder();
        for (Equipment eq : equipmentInfoList) {
            equipment.append(eq.getEquipmentName()).append(",");
        }
        if (equipment.length() > 0) {
            experiment.setEquipment(equipment.deleteCharAt(equipment.length() - 1).toString());
        }
        /**获取最后一条含水率配置*/
        TemperatureAndHumidity lastInfo = temperatureAndHumidityService.getLastInfo(new TemperatureAndHumidity());
        if (lastInfo != null) {
            experiment.setTemperature(lastInfo.getTemperature());
            experiment.setHumidness(lastInfo.getHumidness());
        }
        return experiment;
    }

    @Override
    public int getAllCount(Experiment entity) {
        return experimentMapper.getListCount(entity);
    }
}
